<?php

// [ 应用类 ]

namespace core;
use core\Hook;
use core\Route;
use core\Loader;
use core\Config;
use core\Response;

class App
{
    public static function run()
    {
        // api允许跨域请求
        Config::get('core.api_cross_request') && allow_cross_request();

        // api请求初始监听
        Hook::listen('api_request_init');

        $module   = Route::get('module');
        $resource = Route::get('resource');
        $method   = Route::get('method');
        ( empty($resource) || empty($method) ) && Response::send(400);
        $class = empty($module) ? "app\\$resource\\$method" : "app\\$module\\$resource\\$method";
        Loader::set($class) || Response::send(404);

        // api请求前监听
        Hook::listen('api_request_before',$class);

        $result = call_user_func_array([$class,'run'],[]);

        // api请求后监听
        Hook::listen('api_request_after',$result);

        is_numeric($result) && Response::send($result);
        is_array($result) && Response::send($result[0],$result[1]);
        Response::send(500);
    }
}
